package cn.frank.framework.king.domain.vo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.Serializable;

/**
 * @description: Result
 * @author: renjiangyang
 * @date 2023/10/2 13:50:00
 * @since 1.8
 */
@ToString
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@ApiModel(description = "返回体")
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    @ApiModelProperty(name = "code", value = "状态码,0成功，1失败,2表单参数不正确，401认证失败，403权限不足，404资源未找到,500服务内部错误")
    private Integer code;

    @ApiModelProperty(name = "msg", value = "提示信息")
    private String msg;

    @ApiModelProperty(name = "data", value = "内容")
    private T data;

    @ApiModelProperty(name = "requestTime", value = "请求时间")
    private String requestTime = DateUtil.now();

    @ApiModelProperty(name = "requestUrl", value = "请求地址")
    private String requestUrl;

    //成功
    public static final Result<Object> SUCCESS = new Result<>(ResultConstant.SUCCESS);
    //失败
    public static final Result<Object> FAIL = new Result<>(ResultConstant.FAIL);
    //404未找到资源
    public static final Result<Object> NOFOUNT = new Result<>(ResultConstant.NOFOUND);
    //未登录
    public static final Result<Object> NOTAUTH = new Result<>(ResultConstant.NOAUTH);
    //资源权限不足
    public static final Result<Object> NOTAUTHIR = new Result<>(ResultConstant.NOAUTHIR);
    //表单验证失败
    public static final Result<Object> PARAM_VALID_FAIL = new Result<>(ResultConstant.PARAM_VALID_FAIL);

    public Result(ResultConstant resultConstant) {
        this.code = resultConstant.getCode();
        this.msg = resultConstant.getMsg();
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> success(T data) {
        Result<T> success = new Result<>(ResultConstant.SUCCESS);
        success.setData(data);
        return success;
    }

    public static <T> Result<T> fail(String msg) {
        Result<T> fail = new Result<>(ResultConstant.FAIL);
        fail.setMsg(msg);
        return fail;
    }

    public static <T> Result<T> diy(int code, String msg) {
        Result<T> resultEntity = new Result<>(code, msg);
        return resultEntity;
    }

    public String getRequestUrl() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (ObjectUtil.isNotNull(servletRequestAttributes) && ObjectUtil.isNotNull(servletRequestAttributes.getRequest())) {
            return servletRequestAttributes.getRequest().getRequestURI();
        } else {
            return requestUrl;
        }
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }


}

